package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Cc
 * @version 1.0
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    private DtsRoleMapper dtsRoleMapper;

    @Override
    public Set<String> queryByIds(Integer[] roleIds) {
        Set<String> roles = new HashSet<String>();
        if (roleIds == null || roleIds.length == 0) {
            return roles;
        }

        //设置查询条件
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        wapper.in("id", Arrays.asList(roleIds));
        wapper.eq("deleted", false);
        wapper.eq("enabled", true);

        //查询
        List<DtsRole> roleList = dtsRoleMapper.selectList(wapper);

        return roleList.stream().map(DtsRole::getName).collect(Collectors.toSet());
//        for (DtsRole role : roleList) {
//            roles.add(role.getName());
//        }

//        return roles;
    }

    @Override
    public List<DtsRole> findRoleList() {
        return dtsRoleMapper.selectList(null);
    }

    @Override
    public IPage<DtsRole> querySelective(String name, Integer page, Integer limit, String sort, String order) {
        return dtsRoleMapper.selectPage(new Page(page,limit),
                new QueryWrapper<DtsRole>()
                        .eq(!StringUtils.isEmpty(name),"name",name)
                        .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort)

        );}
}
